/**
 * 
 */
package org.vsg.stock.job;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import java.util.Vector;

import org.apache.commons.io.IOUtils;
import org.apache.commons.io.LineIterator;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.dao.DataAccessException;
import org.vsg.common.exceptions.service.ServiceException;
import org.vsg.stock.basic.StockHelper;
import org.vsg.stock.basic.domain.StockDailyLine;
import org.vsg.stock.basic.persistence.StockDailyLineDao;
import org.vsg.stock.extension.service.StockMigrationService;

/**
 * @author bill vison
 *
 */
public class StockDailyLineImportDataJob implements Job {

	private static final Logger logger = LoggerFactory.getLogger(StockDailyLineImportDataJob.class);	
	
	private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	
	private DecimalFormat decFormat = new DecimalFormat("##0.00000");
	
	private DecimalFormat decFormat2 = new DecimalFormat("##0.0000");
	
	private StockHelper helper = StockHelper.getInstance();
	
	/* (non-Javadoc)
	 * @see org.quartz.Job#execute(org.quartz.JobExecutionContext)
	 */
	@Override
	public void execute(JobExecutionContext context) throws JobExecutionException {
		// TODO Auto-generated method stub
		JobDataMap dm = context.getMergedJobDataMap();
		Properties config = (Properties)(dm.get( "APPLICATION_SYSTEM_CONFIGRATION" ));
		
		// --- reade the spring context ---
		String[] configContextFiles = {
				"classpath*:spring/**/configurationContext-quartz.xml",
				"classpath*:spring/**/applicationContext.xml",
				"classpath*:spring/**/dataAccessContext.xml"
		};

		
		GenericApplicationContext ctx = new GenericApplicationContext();
		ctx.registerBeanDefinition( "propertyConfigurer" , dynamicBeanDefined(config)  );
		XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(ctx);
		xmlReader.loadBeanDefinitions(configContextFiles);
	
		ctx.refresh();
		
		// ---- get the context bean ---
		File dataDir = new File(config.getProperty("stock.daily.dir"));
		
		if (logger.isDebugEnabled() ) {
			logger.debug( "log in directory : " + dataDir.getAbsolutePath() );
		}		
		
		workJob(ctx , dataDir);
		
	}

	private BeanDefinition dynamicBeanDefined(Properties props) {
		MutablePropertyValues mpv = new MutablePropertyValues();
		mpv.addPropertyValue("properties", props);
		RootBeanDefinition ged = new RootBeanDefinition(PropertyPlaceholderConfigurer.class , mpv);
		return ged;
	}
	
	private void workJob(ApplicationContext ctx , File dir) {
		Calendar today = Calendar.getInstance();
		Date todayDate = today.getTime();
		File file = new File(dir, sdf.format( todayDate )+".txt");
		
		if (!helper.isTradingDate( todayDate ) || !file.exists()) {
			
			if (!file.exists()) {
				logger.info("File does not exist : " + file);
			}
			
			return ;
		}
		
		StockMigrationService stockMigrationService = (StockMigrationService)ctx.getBean("stockMigrationService");
		
		try {
			stockMigrationService.updateDailyLine(file);
		} catch (ServiceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		
	}
	
}
